#include "stdafx.h"
#include <conio.h>
#include "BinaryTree.h"
//////////////////////////////////////////////////////////////////////////
 
//////////////////////////////////////////////////////////////////////////
// int main()
// {
// 	tree_node *head = tnode_construct(NULL);
// 	head->data = 1342;
// 	head->right = tnode_construct(head);
// 	head->right->data = 79;
// 	tnode_add(head->right, 19034);
// 
// 	FILE *out = fopen("tree.txt", "w+");
// 	tree_print(head, out);
// 	fclose(out);
// 	tree_destruct(head);
// 
// 	FILE *in = fopen("tree.txt", "r");
// 	tree_node* salvagedhead = tree_scan(in);
// 	fclose(in);
// 
// 	FILE *newout = fopen("checktree.txt", "w+");
// 	tree_print(salvagedhead, newout);
// 	fclose(newout);
// 	tree_destruct(salvagedhead);
// 	return 0;
// }
////////////////////////////////////////////////////////////////////////// 

////////////////////////////////////////////////////////////////////////// 
int main() {
	return 0;
}
//////////////////////////////////////////////////////////////////////////
tree_node* tnode_construct(tree_node* prev) 
{
	tree_node *creatednode = (tree_node*)calloc(1, sizeof(tree_node));
	creatednode->left = creatednode->right = NULL;
	creatednode->parent = prev;
	creatednode->data = 0;

	return creatednode;
}

void tnode_destruct(tree_node* todelete)
{
	assert(("TNode_Destruct has received a valid node to delete"));
	todelete->left = todelete->right = todelete->parent = NULL;
	todelete->data = 0;
	free(todelete);
}

void tree_destruct(tree_node* headtodeletefrom)
{
	assert(("PRE: [sub]tree_destruct received a valid [sub]tree_head", headtodeletefrom != NULL));
	if (headtodeletefrom->left) tree_destruct(headtodeletefrom->left);
	if (headtodeletefrom->right) tree_destruct(headtodeletefrom->right);
	tnode_destruct(headtodeletefrom);
}
//////////////////////////////////////////////////////////////////////////
int tnode_add_mt(tree_node* nodetoglueto)
{
	assert(("PRE: TNode_Add_Empty has received a valid node to glue to", nodetoglueto != NULL));

	if (!nodetoglueto->left) {
		nodetoglueto->left = tnode_construct(nodetoglueto);
		return 0;
	} else if (!nodetoglueto->right) {
		nodetoglueto->right = tnode_construct(nodetoglueto);
		return 1;
	} else
		return -1;
}

int tnode_add(tree_node* nodetoglueto1, tree_data tostore) 
{
	assert(("PRE: TNode_Add[_a_value] has received a valid node to glue to", nodetoglueto1 != NULL));
	int out = tnode_add_mt(nodetoglueto1);

	if (out == 0)
		nodetoglueto1->left->data = tostore;
	else if (out == 1)
		nodetoglueto1->right->data = tostore;

	return out;
}
//////////////////////////////////////////////////////////////////////////
void tree_print(tree_node* headtoprintfrom, FILE* outfile)
{
	assert(("PRE: Tree_Print has received a valid subtree head to print from", headtoprintfrom != NULL));
	
	fprintf(outfile, "( ");

	if (headtoprintfrom->left)
		tree_print(headtoprintfrom->left, outfile);
	else
		fprintf(outfile, "nil");

	fprintf(outfile, " %ld ", headtoprintfrom->data);

	if (headtoprintfrom->right)
		tree_print(headtoprintfrom->right, outfile);
	else
		fprintf(outfile, "nil");

	fprintf(outfile, " )");

}

tree_node* tree_scan(FILE* infile)
{
	char container[35] = {};
	fscanf(infile, " %s", container);
	if (strcmp(container, "nil") == 0) {
		DOUT("RETURNED!");
		return NULL;
	}

	tree_node *scannedhead = tnode_construct(NULL);
	
	//DOUT("WE'LL ENTER LEFTSCAN NOW");
	scannedhead->left = tree_scan(infile);
	
	fscanf(infile, " %ld ", &(scannedhead->data));

	//DOUT("WE'LL ENTER RIGHTSCAN NOW");
	scannedhead->right = tree_scan(infile);

	fscanf(infile, " )");
	return scannedhead;
}
